import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class HomeBanner extends StatefulWidget {
  HomeBanner({super.key, required this.bannerList});

  // 定义列表接收父组件传值
  final List bannerList;

  @override
  State<HomeBanner> createState() => _HomeBannerState();
}

class _HomeBannerState extends State<HomeBanner> {
  @override
  Widget build(BuildContext context) {
    if (widget.bannerList.isNotEmpty) {
      return CarouselSlider(
        items: widget.bannerList
            .map(
              (item) => GestureDetector(
                  onTap: () {
                    if (kDebugMode) {
                      print('点击了, id:${item['imgName']}');
                    }
                  },
                  child: Container(
                    margin: const EdgeInsets.all(5.0),
                    child: ClipRRect(
                      borderRadius:
                          const BorderRadius.all(Radius.circular(5.0)),
                      child: Image.network(item['imgUrl'],
                          fit: BoxFit.cover, width: 1000.0),
                    ),
                  )),
            )
            .toList(),
        options: CarouselOptions(
          height: 200,
          enlargeCenterPage: true,
          autoPlay: true,
        ),
      );
    } else {
      return Container();
    }
  }
}
